var Xinge = require('./util/Xinge');

var XingeAppIos = new Xinge.XingeApp(2200271721, '00cd709460efe0e9429eb8488f5c3ce2');
var XingeAppAnd = new Xinge.XingeApp(2100271997, '70070e730574d5752e9dcca6078fe8c7');
var XingeJNTBIos = new Xinge.XingeApp(2200278519, '5af63fa208baa1110c55d40b4687fafd');
var XingeJNTBAnd = new Xinge.XingeApp(2100278449, '8a3ef42ed3b40abd5e3cf9b8f649808a');
var XingeApp = {ios:XingeAppIos, and:XingeAppAnd, jntbIos:XingeJNTBIos, jntbAnd:XingeJNTBAnd};
module.exports = XingeApp;

XingeApp.pushSimple2Account = function (pushPlat,account, message, callback) {

    var info = {};
    if (pushPlat == 'jntb') {
        info.packageName = 'com.jntb';
        info.title = '金牛提醒您';
        info.iOSKey = 'jntbIos';
        info.andKey = 'jntbAnd';
    } else {
        info.packageName = 'com.usuz';
        info.title = '微淘客提醒您';
        info.iOSKey = 'ios';
        info.andKey = 'and';
    }
    basePush(info,account,message,callback);
};

function basePush(info,account,message, callback) {
    var iOSMessage = new Xinge.IOSMessage();

    iOSMessage.alert = message;
    iOSMessage.badge = 1;
    // iOSMessage.acceptTime.push(new Xinge.TimeInterval(0, 0, 23, 0));
    // iOSMessage.customContent = {};

    var style = new Xinge.Style();
    style.ring = 1;
    style.vibrate = 1;
    style.lights = 1;
    style.builderId = 77;
    style.nId = 0;

    var action = new Xinge.ClickAction();
    action.actionType = Xinge.ACTION_TYPE_ACTIVITY;
    action.packageName.packageName = info.packageName;
    action.packageName.confirm = 0;

    var androidMessage = new Xinge.AndroidMessage();
    androidMessage.type = Xinge.MESSAGE_TYPE_NOTIFICATION;
    androidMessage.action = action;
    androidMessage.title = info.title;
    androidMessage.content = message;
    androidMessage.style = style;
    androidMessage.sendTime = parseInt(new Date().getTime() / 1000);
    androidMessage.expireTime = 0;
    androidMessage.multiPkg = 1;

    XingeApp[info.iOSKey].pushToSingleAccount(account, iOSMessage, Xinge.IOS_ENV_PRO, function(err, result){
        if(err){
            callback(err, result);
            return;
        }
        XingeApp[info.andKey].pushToSingleAccount(account, androidMessage, function(err1, result1){
            callback(err1, result1);
        });
    });
}